package com.java.base.object;

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;
import sun.misc.Unsafe;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.Field;

@SuppressWarnings("all")
public class MemObj {

    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static void main(String[] args) throws Throwable {
        test5();

    }

    public static void test6() {
        int[] is = new int[2];
    }

    public static void test5() {
//        System.out.println(ClassLayout.parseInstance(new int[10]).toPrintable());
        System.out.println(ClassLayout.parseInstance(new int[25]).toPrintable());
        System.out.println(ClassLayout.parseInstance(new Obj[25]).toPrintable());
        System.out.println(ClassLayout.parseInstance(new Obj[12]).toPrintable());
        System.out.println(ClassLayout.parseInstance(new Obj()).toPrintable());
        System.out.println(ClassLayout.parseInstance(new Obj()).toPrintable());

    }

    public static void test4() {
//        System.out.println(VM.current().details());
        Obj obj = new Obj();
        Obj[] objs = new Obj[1];
//        System.out.println(ClassLayout.parseClass(Obj.class).toPrintable());

        System.out.println("------------------------");
        System.out.println(ClassLayout.parseInstance(1).toPrintable());
        System.out.println("------------------------");
        System.out.println(ClassLayout.parseInstance(new int[2]).toPrintable());

    }

    public static void test3() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        Field field = Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        Unsafe unsafe = (Unsafe) field.get(null);
        Obj obj = (Obj) unsafe.allocateInstance(Obj.class);
        long addr = VM.current().addressOf(obj);
        System.out.println(addr);
//        System.out.println(System.identityHashCode(addr));
//        System.out.println(obj);
        long size = VM.current().sizeOf(obj);
        System.out.println(size);
        System.out.println(VM.current().sizeOf(new Object()));

    }

    public static void test1() throws NoSuchFieldException, IllegalAccessException, InstantiationException {
        Field field = Unsafe.class.getDeclaredField("theUnsafe");
        field.setAccessible(true);
        Unsafe unsafe = (Unsafe) field.get(null);

        long addr = unsafe.allocateMemory(1);
        System.out.println(unsafe.getByte(addr));

        byte b = (byte) 1;
        unsafe.putByte(addr, b);
        System.out.println(unsafe.getByte(addr));
        unsafe.freeMemory(addr);
        System.out.println(unsafe.getByte(addr));

    }

    public static void test2() throws Throwable {
        Obj o = new Obj();
        System.out.println(o);
        o.finalize();
        Thread.sleep(1000);
        System.out.println(o);
    }

    public static class Obj {

        int i = 32;
//        boolean b = true;
//        boolean b1 = true;
//        boolean b2 = true;
        short s = 0;



        String str = "d";
        Object obj;

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


}

